package cn.hanyx.coder.design.pattern.structural.bridge.demo01;

import cn.hanyx.coder.design.pattern.structural.bridge.demo02.handler.AlertHandler;
import cn.hanyx.coder.design.pattern.structural.bridge.info.AlertRule;
import cn.hanyx.coder.design.pattern.structural.bridge.info.Notification;
import cn.hanyx.coder.design.pattern.structural.bridge.info.NotificationEmergencyLevel;

import java.util.ArrayList;
import java.util.List;

public class Alert01 {
    private AlertRule rule;
    private Notification notification;
    private List<AlertHandler> alertHandlers = new ArrayList<>();

    public Alert01(AlertRule rule, Notification notification) {
        this.rule = rule;
        this.notification = notification;
    }

    public void addAlertHandler(AlertHandler alertHandler) {
        this.alertHandlers.add(alertHandler);
    }


    public void check(String api, long requestCount, long errorCount, long durationOfSeconds, long timeoutCount) {
        long tps = requestCount / durationOfSeconds;
        if (tps > rule.getMatchedRule(api).getMaxTps()) {
            notification.notify(NotificationEmergencyLevel.URGENCY, "...");
        }
        if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
            notification.notify(NotificationEmergencyLevel.SEVERE, "...");
        }

        long timeoutTps = timeoutCount / durationOfSeconds;
        if (timeoutTps > rule.getMatchedRule(api).getMaxTimeoutTps()) {
            notification.notify(NotificationEmergencyLevel.URGENCY, "...");
        }
    }
}